package com.mycompany.inventory.persistence;

import com.mycompany.common.AbstractDao;
import com.mycompany.common.jdbc.ConnectionDatabase;
import com.mycompany.inventory.pojo.AddressOwner;

import java.util.ArrayList;
import java.util.List;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by My Company.
 * User: loitran
 * Date: Feb 29, 2012
 * Time: 1:53:19 PM
 * com.mycompany.inventory.persistence.
 */
public class AddressOwnerDAO extends AbstractDao<AddressOwner>{
	
	private Connection con = this.connect;
	public static final String DELETE_SQL = "DELETE FROM oce_owner_address WHERE vendor_id = ?";
	public static final String SELECT_ALL_SQL = "select * from oce_owner_address";
	public static final String SELECT_SQL = "select * from oce_owner_address where address_id = ?";	
	public static final String INSERT_SQL = "INSERT INTO oce_owner_address  (ownerAddress_id, vendor_id, address_id, ownertype, addresstype) VALUES (?,?,?,?,?)";
	public static final String UPDATE_SQL = "UPDATE oce_owner_address SET vendor_id = ?, address_id = ?, ownertype = ?, addresstype = ? where ownerAddress_id = ?";
	
	public AddressOwner create(AddressOwner obj){
		try{
			PreparedStatement ps = con.prepareStatement(INSERT_SQL, PreparedStatement.RETURN_GENERATED_KEYS);
			long id = 0l;
			ps.setLong(1,id);
			ps.setLong(2, Long.parseLong(obj.getVendorId()));
			ps.setLong(3, Long.parseLong(obj.getAddressId()));
			ps.setString(4, obj.getOwnerType());
			ps.setString(5, obj.getAddressType());		
			
			ConnectionDatabase.executeUpdate(ps);	
			ResultSet rs=ps.getGeneratedKeys();
            if(rs.next()){
                Integer Id = (Integer) rs.getInt(1);               
                obj.setId(String.valueOf(Id));
            }
			ps.close();			
			
		}catch (Exception e) {
			// TODO: handle exception
			obj = null;
		}
		return obj;
	}
	
	public AddressOwner update(AddressOwner obj){
		try{
			PreparedStatement ps = con.prepareStatement(UPDATE_SQL, PreparedStatement.RETURN_GENERATED_KEYS);
			long id = Long.parseLong(obj.getId());
			ps.setLong(5,id);
			ps.setLong(1, Long.parseLong(obj.getVendorId()));
			ps.setLong(2, Long.parseLong(obj.getAddressId()));
			ps.setString(3, obj.getOwnerType());
			ps.setString(4, obj.getAddressType());		
			
			ConnectionDatabase.executeUpdate(ps);				
			ps.close();			
		}catch (Exception e) {
			// TODO: handle exception
		}
		return obj;
	}
	
	public AddressOwner find(long id){
		List<AddressOwner> addresses = new ArrayList<AddressOwner>();
		try{
		}catch (Exception e) {
			// TODO: handle exception
		}
		return addresses.size()== 0 ? null : addresses.get(0);
	}
	
	public List<AddressOwner> findAll(){
		List<AddressOwner> addresses = new ArrayList<AddressOwner>();
		try{		
		}catch (Exception e) {
			// TODO: handle exception
			addresses = null;
		}
		return addresses;
	}
	
	
	public void delete(AddressOwner obj){
		try{
			PreparedStatement ps = con.prepareStatement(DELETE_SQL, PreparedStatement.RETURN_GENERATED_KEYS);
			ps.setLong(1,Long.parseLong(obj.getVendorId()));
			ConnectionDatabase.executeUpdate(ps);	
			ps.close();		
		}catch (Exception e) {
			obj = null;
		}
	}
	
	private List<AddressOwner> convert(ResultSet rs) throws SQLException {
		List<AddressOwner> addresses = new ArrayList<AddressOwner>();
		return addresses;
	}
}
